/*
* Copyright (c) 2024 -present FengJinjian ( 751965763@qq.com )
*/
import { request } from "@/utils/request/index";
import type {ListResult} from "@/api/model/listModel";

// 合同相关接口
export interface ContractInfo {
  id?: number;
  name: string;
  type: number;
  contractType: number;
  paymentType: number;
  amount: number;
  partyA: string;
  partyB: string;
  signDate: string;
  startDate: string;
  endDate: string;
  files?: Array<any>;
  comment?: string;
  status?: number;
  adminName?: string;
}

// API响应接口
export interface ApiResponse<T> {
  code: number;
  message: string;
  data: T;
}

// 创建合同响应
export type CreateContractResponse = ApiResponse<number>; // 新创建的合同ID

// 更新合同请求
export interface UpdateContractRequest {
  id: number;
  name?: string;
  type?: number;
  contractType?: number;
  paymentType?: number;
  amount?: number;
  partyA?: string;
  partyB?: string;
  signDate?: string;
  startDate?: string;
  endDate?: string;
  files?: Array<any>;
  comment?: string;
  status?: number;
  adminName?: string;
}

// 创建合同
export async function createContract(data: ContractInfo): Promise<CreateContractResponse> {
  const ret = await request.post<CreateContractResponse>({ url: '/contract', data });
  console.log('createContract response:', ret);
  return ret;
}
export function getList() {
  return request.get<ListResult>({
    url: '/contract/page',
  });
}
// 分页响应接口
export interface PageResponse<T> {
  records: T[];
  total: number;
  size: number;
  current: number;
}

// 获取合同列表
export async function getContractList(params: Record<string, any>) {
  const { current, pageSize, ...rest } = params;
  return request.get<ApiResponse<PageResponse<ContractInfo>>>({
    url: '/contract/page',
    params: {
      pageNum: current,
      pageSize,
      ...rest
    }
  });
}

// 更新合同
export async function updateContract(data: UpdateContractRequest): Promise<ApiResponse<void>> {
  return request.put({
    url: `/contract/${data.id}`,
    data
  });
}

// 删除合同
export async function deleteContract(id: number): Promise<ApiResponse<void>> {
  return request.delete({
    url: `/contract/${id}`
  });
}

// 获取单个合同详情
export async function getContractDetail(id: number): Promise<ApiResponse<ContractInfo>> {
  return request.get({
    url: `/contract/${id}`
  });
}

// 批量删除合同
export async function batchDeleteContracts(ids: number[]): Promise<ApiResponse<void>> {
  return request.delete({
    url: '/contract/batch',
    data: ids
  });
}
